import router from '@system.router';
import storage from '@system.storage';
import prompt from '@system.prompt';


export default {
    data: {
        list:[]
    },
    onInit() {
        storage.get({
            key:'isOne',
            success:(isOne)=>{
                if(!(isOne)){
                    storage.set({
                        key:'isOne',
                        value:'1'
                    })
                    storage.set({
                        key:'list',
                        value:JSON.stringify({list:[{"title":"首次使用","info":"欢迎使用本工具","id":1}]}),
                        success:()=>{
                            this.getInfo()
                        }
                    })
                }else{
                    this.getInfo()
                }
            }
        })
    },
    onShow(){
        this.getInfo()
    },
    getInfo(){
        storage.get({
            key:'list',
            success:(res)=>{
                let data = JSON.parse(res)
                this.list = data.list
            }

        })
    },
    tapItem(e){
        router.push ({
            uri: 'pages/detail/detail',
            params:{
                'mainid':e.id
            }
        });

    },
    del(e){
        prompt.showDialog({
            title:'提示',
            message:'确认删除?',
            buttons:[
                {
                    text:'确认',
                    color:'#ff0000',
                },{
                    text:'取消',
                    color:'#0078ff',
                },],
            success:(is)=>{
                if(is.index == 0){

                    let newList = []
                    for(let i of this.list){
                        if(i.id != e.id){
                            newList = [...newList,i]
                        }
                    }
                    storage.set({
                        key:'list',
                        value:JSON.stringify({list:newList}),
                        success:()=>{
                            prompt.showToast({
                                message:'删除成功'
                            })
                            this.getInfo()
                        }
                    })
                }
            }

        })

//

    },
    topage(){
        router.push ({
            uri: 'pages/detail/detail',
        });
    },

}
